package com.cmccgz.udacs_landlord_webapp.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseHolder;
import com.cmccgz.baselibrary.base.NoDataViewHolder;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.DeviceInfoBean;
import com.cmccgz.udacs_landlord_webapp.main.unlock.BtUnlockContract;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class LeDeviceListAdapter extends RecyclerView.Adapter<BaseHolder> {

    private List<DeviceInfoBean> mLeDevices;
    private Context mContext;
    private BtUnlockContract.Presenter presenter;

    public LeDeviceListAdapter(Context context, BtUnlockContract.Presenter presenter) {
        this.mContext = context;
        this.presenter = presenter;
    }

    public LeDeviceListAdapter(Context context, BtUnlockContract.Presenter presenter, List<DeviceInfoBean> list) {
        this.mContext = context;
        this.presenter = presenter;
        this.mLeDevices = list;
    }

    public void addDevice(BluetoothDevice device) {
        if (mLeDevices == null) {
            mLeDevices = new ArrayList<>();
        }
        DeviceInfoBean newBean = null;
        Iterator<DeviceInfoBean> iterator = mLeDevices.iterator();
        while (iterator.hasNext()) {
            DeviceInfoBean bean = iterator.next();
            if (bean.device_name.equals(device.getName())) {
                newBean = bean;
                iterator.remove();
                break;
            }
        }
       if (newBean!=null){
           newBean.mac = device.getAddress();
           newBean.isConnect = true;
       }
        mLeDevices.add(0, newBean);
        notifyDataSetChanged();
    }

    public void removeDevice(BluetoothDevice device) {
        if (mLeDevices == null) {
            mLeDevices = new ArrayList<>();
        }
        DeviceInfoBean newBean = null;
        Iterator<DeviceInfoBean> iterator = mLeDevices.iterator();
        while (iterator.hasNext()) {
            DeviceInfoBean bean = iterator.next();
            if (bean.device_name.equals(device.getName())) {
                newBean = bean;
                iterator.remove();
                break;
            }
        }
        if (newBean!=null){
            newBean.mac = device.getAddress();
            newBean.isConnect = false;
            mLeDevices.add(newBean);
        }
        notifyDataSetChanged();
    }

    public void clear(){
        if (mLeDevices == null){
            mLeDevices  = new ArrayList<>();
        }
        mLeDevices.clear();
        notifyDataSetChanged();
    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            NoDataViewHolder noDataViewHolder = new NoDataViewHolder(LayoutInflater.from(mContext).inflate(R.layout.lv_empty_view, parent, false));
            noDataViewHolder.setText("附近暂无设备");
            return noDataViewHolder;
        } else if (viewType == 1) {
            return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_device, parent, false));
        } else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(BaseHolder holder, int position) {
        holder.instanceView(position);
    }

    @Override
    public int getItemViewType(int position) {
        if (mLeDevices == null) {
            return -1;
        } else if (mLeDevices.size() == 0) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public int getItemCount() {
        if (mLeDevices == null) {
            return 0;
        } else if (mLeDevices.size() == 0) {
            return 1;
        } else {
            return mLeDevices.size();
        }
    }

    class ViewHolder extends BaseHolder {

        private TextView name;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void assignViews() {
            name = (TextView) findViewById(R.id.tv_name);
        }

        @Override
        public void instanceView(int position) {
            if (mLeDevices != null) {
                final DeviceInfoBean device = mLeDevices.get(position);
                name.setText(device.house_name);
                getItemView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                        builder.setTitle("提示").setMessage("确定解锁该门禁设备吗？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                presenter.unlock(device.house_name, device.mac);
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builder.create().show();
                    }
                });
                getItemView().setEnabled(device.isConnect);
            }
        }
    }
}
